package com.system.healthproject.model.doctor.controller;

import com.system.healthproject.annotation.RequireToken;
import com.system.healthproject.bean.*;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.admin.paramsbean.IllDiscussInfo;
import com.system.healthproject.model.doctor.service.DoctorIlldiscussService;
import com.system.healthproject.pojo.DiscussSingup;
import com.system.healthproject.pojo.Doctor;
import com.system.healthproject.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Xuheng Zhou
 * @date 2020/6/16 9:22
 * @desc
 */
@Controller
@RequestMapping("/doctor/Illdiscuss")
@Api(value = "医生端-病历探讨模块",tags = "医生端-病历探讨管理API")
public class DoctorIlldiscussController {

    @Autowired
    private DoctorIlldiscussService service;


    @Autowired
    RedisUtil redisUtil;

    @RequireToken
    @ApiOperation(value = "医生查询全部列表病历探讨信息接口(已调试)",notes = "医生查询全部列表病历探讨信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorIlldiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页数",dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "页数显示数据条数",dataType = "Integer")})
    public ApiResult<PagesBean<DoctorIlldiscussBean>> selectDoctorIlldiscuss(@RequestHeader("token") String token,
                                                                             @RequestParam(name = "page") Integer page ,
                                                                             @RequestParam(name = "size") Integer size){

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);

        List<DoctorIlldiscussBean> list=service.selectDoctorIlldiscuss(doctorId,page,size);

        PagesBean pagesBean=new PagesBean();
        pagesBean.setData(list);
        pagesBean.setPageSize(size);
        pagesBean.setNowsPage(page);
        return ApiResult.successWithData("查询成功",pagesBean);
    }


    @RequireToken
    @ApiOperation(value = "查询医生创建报名的病历探讨信息接口（已调试）",notes = "查询医生创建报名的病历探讨信息",httpMethod = "post")
    @RequestMapping(value = "/selectSpecificDoctorIlldiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页数",dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "页数显示数据条数",dataType = "Integer")})
    public ApiResult<PagesBean<DoctorIlldiscussBean>> selectSpecificDoctorIlldiscuss(@RequestHeader("token") String token,
                                                                                             @RequestParam(name = "page") Integer page ,
                                                                                             @RequestParam(name = "size") Integer size){

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);

        List<DoctorIlldiscussBean> list=service.selectSpecificDoctorIlldiscuss(doctorId,page,size);

        PagesBean pagesBean=new PagesBean();
        pagesBean.setData(list);
        pagesBean.setPageSize(size);
        pagesBean.setNowsPage(page);
        return ApiResult.successWithData("查询成功",pagesBean);
    }

    @ApiOperation(value = "查询医生付费类的病历探讨详情信息接口（已调试）",notes = "查询医生付费的某个病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorOnePayIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)
    public ApiResult<DoctorPayOneIllDiscuss> selectDoctorOnePayIllDiscuss(@RequestParam Integer illdiscussId){

        System.out.println(illdiscussId);


        DoctorPayOneIllDiscuss doctorPayOneIllDiscuss = new DoctorPayOneIllDiscuss();
        //获取查询的数据
        doctorPayOneIllDiscuss=service.selectPayOneIllDiscussById(illdiscussId);

        ApiResult apiResult=new ApiResult();
        if (doctorPayOneIllDiscuss!=null){
            apiResult.setMsg("查询详情成功");
            apiResult.setCode(200);
            apiResult.setData(doctorPayOneIllDiscuss);
        }else {
            apiResult.setMsg("查询详情失败");
            apiResult.setCode(500);
        }

        return apiResult;
    }

    @RequireToken
    @ApiOperation(value = "查询医生想要参与付费类的病历探讨信息接口（已调试）",notes = "查询医生参与的付费病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorJoinOnePayIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)
    public ApiResult<DoctorJoinPayOneIllDiscuss> selectDoctorJoinOnePayIllDiscuss(@RequestHeader("token") String token,
                                                                          @RequestParam Integer illdiscussId){

        System.out.println(illdiscussId);
        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);
        DoctorJoinPayOneIllDiscuss joinPayOneIllDiscuss = new DoctorJoinPayOneIllDiscuss();
        //获取查询的数据
        joinPayOneIllDiscuss=service.selectJoinPayOneIllDiscussById(illdiscussId,doctorId);

        ApiResult apiResult=new ApiResult();

        if (joinPayOneIllDiscuss!=null){
            apiResult.setMsg("查询详情成功");
            apiResult.setCode(200);
            apiResult.setData(joinPayOneIllDiscuss);
        }else {
            apiResult.setMsg("查询详情失败");
            apiResult.setCode(500);
        }

        return apiResult;
    }


    @RequireToken
    @ApiOperation(value = "医生报名参与付费类的病历探讨信息接口(已调试)",notes = "医生报名参与付费病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/DoctorJoinOnePayIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)

    public ApiResult DoctorJoinOnePayIllDiscuss(@RequestHeader("token") String token,
                                                      @RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println(illdiscussId);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);

        //查询报名表里是否用户已经报名
        DiscussSingup check=service.checkIsSignUp(doctorId,illdiscussId);

        ApiResult apiResult = new ApiResult();
        //说明报名表里不存在，及没有报名过
        if (check ==null ) {
            //进行报名操作
            boolean tf = service.addSignUpPayIlldiscuss(doctorId, illdiscussId);

            if (tf==true){
            apiResult.setMsg("报名成功");
            apiResult.setCode(200);
                return apiResult;
            }else{
                apiResult.setMsg("报名失败!可能原因是账户余额不足！或");
                apiResult.setCode(500);
                return apiResult;
            }
        }else{
            apiResult.setMsg("报名失败！您已报名，请勿重复报名");
            apiResult.setCode(500);
            return apiResult;
        }

    }

    @RequireToken
    @ApiOperation(value = "查询医生报名成功后的付费类病历探讨信息接口(已调试)",notes = "查询医生报名成功的付费病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorJoinSuccessOnePayIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)

    public ApiResult<List<DoctorJoinSuccessPayOneIlldiscuss>> selectDoctorJoinSuccessOnePayIllDiscuss(@RequestHeader("token") String token,
                                                @RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println(illdiscussId);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId = (Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId = service.selectDoctorIdByLoginId(loginId);

        List<DoctorJoinSuccessPayOneIlldiscuss> list=service.selectDoctorJoinSuccessOnePayIllDiscuss(illdiscussId,doctorId);


        ApiResult apiResult=new ApiResult();

        apiResult.setCode(200);
        apiResult.setMsg("查询成功");
        apiResult.setData(list);

        return apiResult;
    }

    @ApiOperation(value = "查询医生奖励类的病历探讨详情信息接口（已调试）",notes = "查询医生奖励某个病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorOneRewardIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)
    public ApiResult<DoctorRewardOneIllDiscuss> selectDoctorOneRewardIllDiscuss(@RequestParam Integer illdiscussId){

        System.out.println(illdiscussId);


        DoctorRewardOneIllDiscuss doctorRewardOneIllDiscuss = new DoctorRewardOneIllDiscuss();
        //获取查询的数据
        doctorRewardOneIllDiscuss=service.selectRewardOneIllDiscussById(illdiscussId);

        ApiResult apiResult=new ApiResult();
        if (doctorRewardOneIllDiscuss!=null){
            apiResult.setMsg("查询详情成功");
            apiResult.setCode(200);
            apiResult.setData(doctorRewardOneIllDiscuss);
        }else {
            apiResult.setMsg("查询详情失败");
            apiResult.setCode(500);
        }

        return apiResult;
    }

    @RequireToken
    @ApiOperation(value = "查询医生想要参与奖励类的病历探讨信息接口（已调试）",notes = "查询医生参与的奖励病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorJoinOneRewardIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)
    public ApiResult<DoctorJoinRewardOneIllDiscuss> selectDoctorJoinOneRewardIllDiscuss(@RequestHeader("token") String token,
                                                                                  @RequestParam Integer illdiscussId){

        System.out.println(illdiscussId);
        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);
        DoctorJoinRewardOneIllDiscuss joinRewardOneIllDiscuss = new DoctorJoinRewardOneIllDiscuss();
        //获取查询的数据
        joinRewardOneIllDiscuss=service.selectJoinRewardOneIllDiscussById(illdiscussId,doctorId);

        ApiResult apiResult=new ApiResult();

        if (joinRewardOneIllDiscuss!=null){
            apiResult.setMsg("查询详情成功");
            apiResult.setCode(200);
            apiResult.setData(joinRewardOneIllDiscuss);
        }else {
            apiResult.setMsg("查询详情失败");
            apiResult.setCode(500);
        }

        return apiResult;
    }


    @RequireToken
    @ApiOperation(value = "医生报名参与奖励的病历探讨信息接口（已调试）",notes = "医生报名参与奖励病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/DoctorJoinOneRewardIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)

    public ApiResult DoctorJoinOneRewardIllDiscuss(@RequestHeader("token") String token,
                                                @RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println(illdiscussId);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId=(Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId =service.selectDoctorIdByLoginId(loginId);

        //查询报名表里是否用户已经报名
        DiscussSingup check=service.checkIsSignUp(doctorId,illdiscussId);

        ApiResult apiResult = new ApiResult();
        //说明报名表里不存在，及没有报名过
        if (check ==null ) {
            //进行报名操作
            boolean tf = service.addSignUpRewardIlldiscuss(doctorId, illdiscussId);

            if (tf==true){
                apiResult.setMsg("报名成功");
                apiResult.setCode(200);
                return apiResult;
            }else{
                apiResult.setMsg("报名失败!");
                apiResult.setCode(500);
                return apiResult;
            }
        }else{
            apiResult.setMsg("报名失败！您已报名，请勿重复报名");
            apiResult.setCode(500);
            return apiResult;
        }

    }

    @RequireToken
    @ApiOperation(value = "查询医生报名成功后的奖励类病历探讨信息接口（已调试）",notes = "查询医生报名成功的奖励病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorJoinSuccessOneRewardIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)

    public ApiResult<List<DoctorJoinSuccessRewardOneIlldiscuss>> selectDoctorJoinSuccessOneRewardIllDiscuss(@RequestHeader("token") String token,
                                                                                                      @RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println(illdiscussId);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId = (Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId = service.selectDoctorIdByLoginId(loginId);

        List<DoctorJoinSuccessRewardOneIlldiscuss> list =service.selectDoctorJoinSuccessOneRewardIllDiscuss(illdiscussId,doctorId);


        ApiResult apiResult=new ApiResult();
        if (list.size()>0) {
            apiResult.setCode(200);
            apiResult.setMsg("查询成功");
            apiResult.setData(list);
        }else {
            apiResult.setCode(500);
            apiResult.setMsg("查询失败");

        }
        return apiResult;
    }

    @ApiOperation(value = "医生结束付费类的病历探讨活动接口(已调试)",notes = "医生结束付费类病历探讨信息",httpMethod = "post")
    @RequestMapping(value = "/doctorClosePayIlldiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @RequireToken
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)
    public ApiResult doctorClosePayIlldiscuss(@RequestHeader("token") String token,
                                              @RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println("---"+illdiscussId);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId = (Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId = service.selectDoctorIdByLoginId(loginId);

        //付款类结束，因为金额已经在报名的时候操作过了，这里付费类的直接关闭就可以了
        boolean tf=service.doctorClosePayIlldiscuss(illdiscussId,doctorId);
        ApiResult apiResult=new ApiResult();
        if (tf){
            apiResult.setCode(200);
            apiResult.setMsg("结束成功");
        }else {
            apiResult.setCode(500);
            apiResult.setMsg("结束失败！可能是活动id输入错误");
        }
        return apiResult;

    }


    @ApiOperation(value = "医生结束奖励类的病历探讨活动接口(已调试)",notes = "医生结束奖励类病历探讨信息",httpMethod = "post")
    @RequestMapping(value = "/doctorCloseRewardIlldiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @RequireToken
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)
    public ApiResult doctorCloseRewardIlldiscuss(@RequestHeader("token") String token,
                                              @RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println("---" + illdiscussId);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId = (Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId = service.selectDoctorIdByLoginId(loginId);

        //查询该病历探讨类型是不是为已结束
        boolean checkEnd = service.checkIsEndById(illdiscussId);

        ApiResult apiResult = new ApiResult();
        //说明该活动id对应的活动不存在结束状态
        if (checkEnd == false) {

            //奖励类结束，需要把每个参会医生的id取到，然后进行金额的分配
            boolean tf = service.doctorCloseRewardIlldiscuss(illdiscussId, doctorId);

            if (tf) {
                apiResult.setCode(200);
                apiResult.setMsg("结束成功");
            } else {
                apiResult.setCode(500);
                apiResult.setMsg("结束失败！可能是活动id输入错误，或者有医生未账号开户，或者该活动不是奖励类型！");
            }
            return apiResult;

        }
        apiResult.setMsg("结束失败！该活动已经结束！");
        apiResult.setCode(500);
        return apiResult;
    }
    @RequireToken
    @ApiOperation(value = "医生发起病历探讨信息接口(已调试)",notes = "医生新增病历探讨信息",httpMethod = "post")
    @RequestMapping(value = "/doctorInsertIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public ApiResult doctorInsertIllDiscuss(@RequestHeader("token") String token,
                                      @RequestBody DoctorAddIllDiscussBean doctorAddIllDiscussBean) {

        System.out.println(doctorAddIllDiscussBean);

        //获取redis中的数据中token
        Map<String, Object> stringObjectMap = (Map<String, Object>) redisUtil.get(token);
        //获取token中保存的医生登录id
        Integer loginId = (Integer) stringObjectMap.get("loginId");

        //通过loginId获取医生id
        Integer doctorId = service.selectDoctorIdByLoginId(loginId);
        doctorAddIllDiscussBean.setDoctorId(doctorId);
        //添加病历探讨的信息
        boolean tf=service.addIllDiscuss(doctorAddIllDiscussBean);

        ApiResult apiResult=new ApiResult();
        if(tf){
            //返回新增病历探讨的id，用于发起成功后的显示信息
            //因为mapper中 设置了useGeneratedKeys，所以添加成功就会把我的自增的活动id返回
            System.out.println(doctorAddIllDiscussBean.getIlldiscussId());
            apiResult.setMsg("发起病历探讨成功");

        }
        return apiResult;

    }


    @ApiOperation(value = "查询医生发起成功后的病历探讨详情信息接口(已调试)",notes = "查询医生发起成功的病历探讨详情信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorAddSuccessOneIllDiscuss",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiImplicitParam(name = "illdiscussId",value = "病历探讨id",dataType = "String",required = true)

    public ApiResult<DoctorAddSuccessOneIlldiscuss> selectDoctorAddSuccessOneIllDiscuss(@RequestParam("illdiscussId") Integer illdiscussId) {

        System.out.println(illdiscussId);


        DoctorAddSuccessOneIlldiscuss doctorAddSuccessOneIlldiscuss =service.selectDoctorAddSuccessOneIllDiscuss(illdiscussId);


        ApiResult apiResult=new ApiResult();
        if (doctorAddSuccessOneIlldiscuss!=null) {
            apiResult.setCode(200);
            apiResult.setMsg("查询成功");
            apiResult.setData(doctorAddSuccessOneIlldiscuss);
        }else {
            apiResult.setCode(500);
            apiResult.setMsg("查询失败");

        }
        return apiResult;
    }

}
